home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER5 / SNDMSGCB.C < prev    next >
C/C++ Source or Header  |  1996-01-14  |  6KB  |  202 lines

  1.  
  2. #include <windows.h>  
  3. #include "SndMsgCb.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. HANDLE hThread = NULL;
  22. DWORD  dwThreadID;
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    // Register the main application window class.
  34.    //............................................
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    // Create the main application window.
  57.    //....................................
  58.    hWnd = CreateWindow( lpszAppName, 
  59.                         lpszTitle,    
  60.                         WS_OVERLAPPEDWINDOW, 
  61.                         CW_USEDEFAULT, 0, 
  62.                         CW_USEDEFAULT, 0,  
  63.                         NULL,              
  64.                         NULL,              
  65.                         hInstance,         
  66.                         NULL               
  67.                       );
  68.  
  69.    if ( !hWnd ) 
  70.       return( FALSE );
  71.  
  72.    ShowWindow( hWnd, nCmdShow ); 
  73.    UpdateWindow( hWnd );         
  74.  
  75.    while( GetMessage( &msg, NULL, 0, 0) )   
  76.    {
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110.  
  111. VOID CALLBACK MessageResult( HWND hWnd, UINT uMsg, DWORD dwData, 
  112.                              LRESULT lResult )
  113. {
  114.    char szMsg[31];
  115.    
  116.    wsprintf( szMsg, "Message Result: %ld", lResult );
  117.    MessageBox( hWnd, szMsg, "Message Done", MB_OK );
  118.  
  119.    *(BOOL*)dwData = TRUE;
  120. }
  121.  
  122.  
  123. VOID NewThreadProc( HGLOBAL hData )
  124. {
  125.    HWND hWnd = (HWND)hData;
  126.    BOOL bMessageProcessed = FALSE;
  127.  
  128.    SendMessageCallback( hWnd, WM_USER, 0, 0, 
  129.                         (SENDASYNCPROC)MessageResult,
  130.                         (DWORD)&bMessageProcessed );
  131.  
  132.    while( !bMessageProcessed );
  133. }
  134.  
  135.  
  136. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  137. {
  138.    switch( uMsg )
  139.    {
  140.        case WM_USER:
  141.                MessageBox( hWnd, "Message was received from thread!",
  142.                                   lpszAppName, MB_OK );
  143.  
  144.                // Return a value back to the callback.
  145.                //.....................................
  146.                return( 20L );
  147.  
  148.        case WM_COMMAND: 
  149.               switch ( LOWORD( wParam ) ) 
  150.               {
  151.                  case IDM_TEST:
  152.                         hThread = CreateThread (NULL, 0,
  153.                                        (LPTHREAD_START_ROUTINE)NewThreadProc,
  154.                                        (LPVOID)hWnd, 0,
  155.                                        (LPDWORD)&dwThreadID);
  156.                         break;
  157.  
  158.                  case IDM_ABOUT :
  159.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  160.                         break;
  161.  
  162.                  case IDM_EXIT :
  163.                         DestroyWindow( hWnd );
  164.                         break;
  165.               }
  166.               break;
  167.       
  168.       case WM_DESTROY :
  169.               PostQuitMessage(0);
  170.               break;
  171.  
  172.       default :
  173.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  174.    }
  175.  
  176.    return( 0L );
  177. }
  178.  
  179.  
  180. LRESULT CALLBACK About( HWND hDlg,           
  181.                         UINT message,        
  182.                         WPARAM wParam,       
  183.                         LPARAM lParam)
  184. {
  185.    switch (message) 
  186.    {
  187.        case WM_INITDIALOG: 
  188.                return (TRUE);
  189.  
  190.        case WM_COMMAND:                              
  191.                if (   LOWORD(wParam) == IDOK         
  192.                    || LOWORD(wParam) == IDCANCEL)    
  193.                {
  194.                        EndDialog(hDlg, TRUE);        
  195.                        return (TRUE);
  196.                }
  197.                break;
  198.    }
  199.  
  200.    return (FALSE); 
  201. }
  202.